package com.talkweb.newproject.common.utils;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by qsf on 2019/4/22.
 */
@Component
public class MapperUtil {
    @Autowired
    Mapper mapper;
    @Autowired
    protected Mapper dozerMapper;

    public<T,S> T map(S s,Class<T> clz) {
        if(s==null) {return null;}
        return this.dozerMapper.map(s,clz);
    }

    public<T,S> List<T> map(List<S> s, Class<T> clz) {
        if(s==null) {
            return null;
        }
        List<T> list = new ArrayList<T>();
        for(S sv: s) {
            if(sv!= null) {
                list.add(this.dozerMapper.map(sv,clz));
            }
        }
        return list;
    }

    public <T,S> Set<T> map(Set<S> s, Class<T> clz) {
        if(s==null) return null;
        Set<T> set = new HashSet<T>();
        for(S ss: s) {
            if(ss!= null) {
                set.add(this.dozerMapper.map(ss,clz));
            }
        }
        return set;
    }

    public <T,S> T[] map(S[] s,Class<T> clz) {
        if(s==null) {
            return null;
        }
        T[] arr = (T[]) Array.newInstance(clz,s.length);
        for(int i = 0;i<s.length;i++) {
            arr[i] = this.dozerMapper.map(s[i],clz);
        }
        return arr;
    }

}
